package com.shine.app.database;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.shine.app.bean.PlaceBean;
import com.shine.app.bean.PlaceNearBean;
import com.shine.app.commons.StringUtils;

public class DataBaseManager {

	private DatabaseHelper databaseHelper;

	public DataBaseManager(Context context) {
		databaseHelper = new DatabaseHelper(context);
	}

	public void add(PlaceNearBean bean) {
		PlaceBean placeBean = bean.getLocation();
		SQLiteDatabase database = databaseHelper.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put("name", placeBean.getFullName());
		values.put("latitude", placeBean.getLatitude());
		values.put("longitude", placeBean.getLongitude());
		values.put("distance", bean.getDistance());
		values.put("stateCode", placeBean.getStateCode());
		values.put("address", placeBean.getAddress1());
		values.put("city", placeBean.getCity());
		values.put("zipCode", placeBean.getZipCode());
		database.insert(DatabaseHelper.TABLE_NAME, null, values);

	}

	public void delete(String name) {
		SQLiteDatabase database = databaseHelper.getWritableDatabase();
		database.delete(DatabaseHelper.TABLE_NAME, "name" + "=?",
				new String[] { name });
	}

	public List<PlaceNearBean> find() {
		List<PlaceNearBean> list = new ArrayList<PlaceNearBean>();
		SQLiteDatabase database = databaseHelper.getReadableDatabase();
		Cursor cursor = database.query(DatabaseHelper.TABLE_NAME, null, null,
				null, null, null, "createdTime desc");
		if (cursor != null) {
			while (cursor.moveToNext()) {
				PlaceNearBean bean = getCourse(cursor);
				list.add(bean);
			}
		}
		return list;

	}

	public boolean findByName(String name) {
		SQLiteDatabase database = databaseHelper.getReadableDatabase();
		Cursor cursor = database.query(DatabaseHelper.TABLE_NAME, null, "name"
				+ "=?", new String[] { name }, null, null, null);
		PlaceNearBean bean = null;
		if (cursor != null && cursor.moveToFirst()) {
			bean = getCourse(cursor);
		}
		if (bean != null
				&& (!StringUtils.isEmpty(bean.getLocation().getFullName()))) {
			return true;
		} else {
			return false;
		}
	}

	private PlaceNearBean getCourse(Cursor cursor) {
		PlaceNearBean bean = new PlaceNearBean();
		PlaceBean placeBean = new PlaceBean();
		placeBean.setFullName(cursor.getString(cursor.getColumnIndex("name")));
		placeBean.setLatitude(cursor.getDouble(cursor
				.getColumnIndex("latitude")));
		placeBean.setLongitude(cursor.getDouble(cursor
				.getColumnIndex("longitude")));
		bean.setDistance(cursor.getDouble(cursor.getColumnIndex("distance")));
		placeBean.setStateCode(cursor.getString(cursor
				.getColumnIndex("stateCode")));
		placeBean
				.setAddress1(cursor.getString(cursor.getColumnIndex("address")));
		placeBean
				.setZipCode(cursor.getString(cursor.getColumnIndex("zipCode")));
		placeBean.setCity(cursor.getString(cursor.getColumnIndex("city")));
		bean.setLocation(placeBean);
		return bean;
	}
}
